package com.itlong.cloud.utils.location;

/**
 * <desc>
 *      定位相关工具类
 * </desc>
 *
 * @createDate 2019/10/11
 */
public class LocationUtil {

    private static double EARTH_RADIUS = 6371.393;      // 地球半径，单位：km

    /**
     * <desc>
     *      角度弧度计算公式
     *      360度=2π π=Math.PI
     *      x度 = x*π/360 弧度
     * </desc>
     *
     * @author wangzhi
     * @createDate 2019/10/11
     */
    private static double getRadian(double degree) {
        return degree * Math.PI / 180.0;
    }

    /**
     * <desc>
     *      根据经纬度计算两点之间的距离
     * </desc>
     *
     * @author wangzhi
     * @createDate 2019/10/11
     */
    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = getRadian(lat1);
        double radLat2 = getRadian(lat2);
        double a = radLat1 - radLat2;// 两点纬度差
        double b = getRadian(lng1) - getRadian(lng2);// 两点的经度差
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1)
                * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        return s * 1000;
    }

}
